import base64
import datetime
import logging

import requests

import logger as my_log
import series_key

my_log.init()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


def main():
    query_variable = "Temperature"
    query_pressure_level = "1"
    query_start_time = datetime.datetime(1979, 1, 1, 0, tzinfo=datetime.timezone.utc).timestamp()
    query_start_time = int(query_start_time)
    query_stop_time = datetime.datetime(1979, 1, 1, 23, tzinfo=datetime.timezone.utc).timestamp()
    query_stop_time = int(query_stop_time)
    start_key = series_key.get_series_key(query_variable, query_pressure_level, query_start_time)
    stop_key = series_key.get_series_key(query_variable, query_pressure_level, query_stop_time)
    response = requests.get("http://localhost:8081/query", params={
        "start_key": base64.b64encode(start_key).decode(),
        "stop_key": base64.b64encode(stop_key).decode(),
    })
    if response.status_code != 200:
        logger.error(f"failed to get datanode of data block: [{response.status_code}] {response.text}")
        return
    print(response.text)


if __name__ == '__main__':
    main()
